function hd = hexdump(av)
% hexdump  - dump hexadecimal values into string
%
% FORMAT:       hd = hexdump(asciivals)
%
% Input fields:
%
%       asciivals   1xN double or char array
%
% Output fields:
%
%       hd          hexdumped string
%
% See also bitdump

% Version:  v0.5c
% Build:    6120415
% Date:     Dec-04 2006, 3:15 PM CET
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
   (~ischar(av) && ...
    ~isa(av, 'double'))
    error( ...
        'BVQXtools:BadArgument', ...
        'Missing or wrong argument.' ...
    );
end

% get lenth and size
l = length(av);
p = 1;

% prepare output
hd = '';

% iterate until end
while (p < l)

    % get stretch
    pa = av(p:min(p+15, l));

    % print into string
    hd = [hd sprintf('%08X:  %-50s %-16s\n', p - 1, ...
        sprintf('%02X ', double(pa)), ...
        saveascii(pa))];

    % go on
    p = p + 16;
end


% internal function to remove ascii characters
function sa = saveascii(av)
    av(av < 32 | av > 127) = 46;
    sa = sprintf('%c', av);
% end of function sa = saveascii(av)
